package spring.factoryBean;

import com.me.common.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.junit.Assert.assertEquals;

/**
 * FactoryBean 测试：启动类
 *
 * @author ME
 * @date 2021/4/8
 */
public class TestFactoryBean {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(FactoryBeanConfig.class);

        // 使用 FactoryBean 获取 spring bean实例
        Student student = (Student) context.getBean("studentFactoryBean");
        System.out.println("Student: " + student);
        assertEquals("Student 继承了`FactoryBean`的 @Scope配置，是单例的。",
                student, context.getBean("studentFactoryBean"));

        // 获取 FactoryBean实例
        StudentFactoryBean studentFactoryBean = (StudentFactoryBean) context.getBean("&studentFactoryBean");
        assertEquals("FactoryBean 的全限定类名为 `spring.factoryBean.StudentFactoryBean`",
                "spring.factoryBean.StudentFactoryBean", studentFactoryBean.getClass().getName());

        ColorEnum red = (ColorEnum) context.getBean("red");
        assertEquals("@Bean 声明的枚举对象（ColorEnum.RED）是单例的。",
                ColorEnum.RED, red);
        assertEquals("@Bean 声明的枚举对象（ColorEnum.RED）成功注入了 Spring工厂",
                context, red.getContext());

        ColorEnum blank = (ColorEnum) context.getBean("blank");
        assertEquals("@Bean 声明的枚举对象（ColorEnum.BLANK）是单例的。",
                ColorEnum.BLANK, blank);
        assertEquals("@Bean 声明的枚举对象（ColorEnum.BLANK）成功注入了 Spring工厂",
                context, blank.getContext());
    }

}
